package br.com.formulavendas.businessusercase;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.formulavendas.domainmodel.Unidade;


/**
 	*
  * @author MyEclipse Persistence Tools 
 */
public class ManterUnidade extends BaseBusinessUserCase 
				implements br.com.formulavendas.businessusercase.baseinterface.IBasePersistence<Unidade>,
				br.com.formulavendas.businessusercase.baseinterface.IBaseFind<Unidade> {
	     
	public ManterUnidade() {
	}

	private static final Logger log = LoggerFactory.getLogger(ManterUnidade.class);
	
	//property constants
	public static final String NOME = "nome";
	public static final String DESCRICAO = "descricao";
	public static final String CNPJ = "cnpj";
    
    @Override
	public void save(Unidade transientInstance) {
    	Transaction tx = this._session.beginTransaction();
        log.debug("saving Unidade instance");
        try {
        	this._session.save(transientInstance);
            tx.commit();
        	log.debug("save successful");
        } catch (RuntimeException re) {
            tx.rollback();
        	log.error("save failed", re);
            throw re;
        }
    }
    
	@Override
	public void delete(Unidade persistentInstance) {
        log.debug("deleting Unidade instance");
        try {
        	this._session.delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    @Override
	public Unidade findById( java.lang.Integer id) {
        log.debug("getting Unidade instance with id: " + id);
        try {
            Unidade instance = (Unidade) this._session
                    .get("formulavendas.domainmodel.Unidade", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Unidade instance) {
        log.debug("finding Unidade instance by example");
        try {
            List results = this._session
                    .createCriteria("formulavendas.domainmodel.Unidade")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    @Override
	public List findByProperty(String propertyName, Object value) {
      log.debug("finding Unidade instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Unidade as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = this._session.createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByNome(Object nome
	) {
		return findByProperty(NOME, nome
		);
	}
	
	public List findByDescricao(Object descricao
	) {
		return findByProperty(DESCRICAO, descricao
		);
	}
	
	public List findByCnpj(Object cnpj
	) {
		return findByProperty(CNPJ, cnpj
		);
	}
	

	@Override
	public List findAll() {
		log.debug("finding all Unidade instances");
		try {
			String queryString = "from Unidade";
	         Query queryObject = this._session.createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Unidade merge(Unidade detachedInstance) {
        log.debug("merging Unidade instance");
        try {
            Unidade result = (Unidade) this._session
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Unidade instance) {
        log.debug("attaching dirty Unidade instance");
        try {
        	this._session.saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Unidade instance) {
        log.debug("attaching clean Unidade instance");
        try {
        	this._session.lock(instance, LockMode.NONE);
          	            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}